﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using LTP.Common;

namespace CJJCH.Web.Manage
{
    public partial class UrlEdit : System.Web.UI.Page
    {
        CJJCH.BLL.T_NET_UrlType bll_urltype = new CJJCH.BLL.T_NET_UrlType();
        CJJCH.BLL.T_NET_URL bll_url = new CJJCH.BLL.T_NET_URL();
        protected void Page_Load(object sender, EventArgs e)
        {
            this.form1.DefaultButton = "btnSave";
            if (!IsPostBack)
            {
                InitRequest();
                InitControl();
                ShowUrl();
            }
            LTP.Common.ButtonClass.ButtonOnClick(this.Page, btnSave, "checkform()", false);
        }

        private void InitControl()
        {
            IList<CJJCH.Model.T_NET_UrlType> list = new List<CJJCH.Model.T_NET_UrlType>();
            list = bll_urltype.GetUrlTypeList();
            for (int i = 0; i < list.Count; i++)
            {
                dropType.Items.Add(new ListItem(list[i].TypeName, list[i].ID.ToString()));
            }
            list = null;
            this.lnkUrl.NavigateUrl = "UrlList.aspx?keytype="+LTP.Common.StringClass.EncodeUrl(this.lblKeyType.Text.Trim());
        }
        private void InitRequest()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["keytype"].ToString())&&LTP.Common.CheckInput.CheckDigit(Request.QueryString["keytype"].ToString()))
            {
                this.lblKeyType.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keytype"].ToString().Trim());
                this.dropType.SelectedValue = this.lblKeyType.Text.Trim();
            }
            if (!string.IsNullOrEmpty(Request.QueryString["ID"].ToString())&&LTP.Common.CheckInput.CheckGUID(Request.QueryString["ID"].ToString()))
            {
                ViewState["ID"] = Request.QueryString["ID"].ToString();
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("操作错误！");
            }
        }
        private void ShowUrl()
        {
            CJJCH.Model.T_NET_URL model = new CJJCH.Model.T_NET_URL();
            model = bll_url.GetUrlModel(new Guid(ViewState["ID"].ToString()));
            if (model!=null)
            {
                this.txtURLName.Text = model.URLName;
                this.txtUrlString.Text = model.URLString;
                this.dropType.SelectedValue = model.TypeID.ToString();
                this.lblImg.Text = model.PicUrl;
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("操作错误！");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            CJJCH.Model.T_NET_URL model = new CJJCH.Model.T_NET_URL();
            string error = checkInput();
            if (string.IsNullOrEmpty(error))
            {
                if (this.fileImg.FileName != "")
                {
                    string ImgType = ConfigHelper.GetConfigString("ImgType");
                    int ImgSize = ConfigHelper.GetConfigInt("UploadFileSize");
                    string UploadPic = ConfigHelper.GetConfigString("UploadPic");
                    string fileName = this.lblImg.Text.Trim();
                    error = FileClass.UploadFile(fileImg, ImgType, UploadPic, ImgSize, ref fileName);
                    if (string.IsNullOrEmpty(error))
                    {
                        model.PicUrl = fileName;
                    }
                    else
                    {
                        LTP.Common.WindowClass.WindowBack(error);
                    }
                }
                else
                {
                    model.PicUrl = this.lblImg.Text.Trim();
                }
                model.ID = new Guid(ViewState["ID"].ToString());
                model.TypeID = Convert.ToInt32(this.dropType.SelectedValue);
                model.URLName = LTP.Common.StringClass.EncodeString(this.txtURLName.Text.Trim());
                model.URLString = LTP.Common.StringClass.EncodeString(this.txtUrlString.Text.Trim());
                string res = bll_url.UpdateUrl(model);
                if (res == "")
                {
                    Response.Redirect("UrlList.aspx?keytype=" + LTP.Common.StringClass.EncodeStr(this.lblKeyType.Text.Trim()));
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack(res);
                }
            }
            else
            {
                LTP.Common.WindowClass.WindowBack(error);
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("UrlList.aspx?keytype=" + LTP.Common.StringClass.EncodeStr(this.lblKeyType.Text.Trim()));
        }
        protected string checkInput()
        {
            StringBuilder error = new StringBuilder();
            if (string.IsNullOrEmpty(this.txtUrlString.Text.Trim()))
            {
                error.Append("请输入链接地址！ \\n");
            }
            if (string.IsNullOrEmpty(this.txtURLName.Text.Trim()))
            {
                error.Append("请输入名称！ \\n");
            }
            if (string.IsNullOrEmpty(this.dropType.SelectedValue))
            {
                error.Append("请选择类别！ \\n");
            }
            else if (!LTP.Common.CheckInput.CheckDigit(this.dropType.SelectedValue))
            {
                error.Append("类别选择错误！ \\n");
            }
            return error.ToString();
        }
    }
}